﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;


namespace ClientePrueba
{
    class Program
    {
        static void Main (string [] args)
        {
            TcpClient tc = new TcpClient();
            tc.Connect("localhost", 4056);

            using (NetworkStream ns = tc.GetStream())
            {
                StreamReader sr = new StreamReader(ns);
                StreamWriter sw = new StreamWriter(ns);

                sw.Write    (construirPOSTRequest("duplicar"));
                sw.Flush    ();

                string renglon = "";

                while (null != (renglon = sr.ReadLine()))
                    Console.WriteLine(renglon);
            }

            tc.Close();

            Console.ReadLine();
        }

        static string construirSOAPRequest (string metodo)
        {
            string inicial =
                "POST /Service1.asmx HTTP/1.1\r\n";

            string headers =
                "From: hav29a@gmail.com\r\n" +
                "Host: localhost\r\n" +
                "Content-Type: text/xml; charset=utf-8\r\n" +
                "Content-Length: [LENGTH]\r\n" +
                "\r\n";

            string body =
                @"<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body><[METODO] xmlns=""http://tempuri.org/"" /></soap:Body></soap:Envelope>";

            body    = body.Replace      ("[METODO]", metodo);
            headers = headers.Replace   ("[LENGTH]", body.Length.ToString());

            string todo = inicial + headers + body;

            return todo;
        }

        static string construirPOSTRequest (string metodo, params string [] @params)
        { 
            string inicial =
                "POST /Service1.asmx/[METODO] HTTP/1.1\r\n";

            string headers =
                "Host: localhost\r\n" +
                "Content-Type: application/x-www-form-urlencoded\r\n" +
                "Content-Length: [LENGTH]\r\n" +
                "\r\n";

            string body = 
                "x=100";

            inicial = inicial.Replace("[METODO]", metodo);
            headers = headers.Replace("[LENGTH]", body.Length.ToString());

            string todo = inicial + headers + body;

            return todo;
        }
    }
}
